package com.ruoyi.information.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.information.domain.InterviewQuestion;
import com.ruoyi.information.domain.vo.DeepQuestionVo;
import com.ruoyi.information.domain.vo.DeepseekResponseData;
import com.ruoyi.information.mapper.InterviewQuestionMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.information.mapper.InterviewRecordMapper;
import com.ruoyi.information.domain.InterviewRecord;
import com.ruoyi.information.service.IInterviewRecordService;

/**
 * 模拟面试记录
Service业务层处理
 * 
 * @author Ly
 * @date 2025-06-16
 */
@Service
public class InterviewRecordServiceImpl implements IInterviewRecordService 
{
    @Autowired
    private InterviewRecordMapper interviewRecordMapper;
    //  面试问题
    @Autowired
    private InterviewQuestionMapper interviewQuestionMapper;

    /**
     * 查询模拟面试记录

     * 
     * @param id 模拟面试记录
主键
     * @return 模拟面试记录

     */
    @Override
    public InterviewRecord selectInterviewRecordById(Long id)
    {
        return interviewRecordMapper.selectInterviewRecordById(id);
    }

    /**
     * 查询模拟面试记录
列表
     * 
     * @param interviewRecord 模拟面试记录

     * @return 模拟面试记录

     */
    @Override
    public List<InterviewRecord> selectInterviewRecordList(InterviewRecord interviewRecord)
    {
        return interviewRecordMapper.selectInterviewRecordList(interviewRecord);
    }

    /**
     * 新增模拟面试记录

     * 
     * @param interviewRecord 模拟面试记录

     * @return 结果
     */
    @Override
    public int insertInterviewRecord(InterviewRecord interviewRecord)
    {
        interviewRecord.setCreateTime(DateUtils.getNowDate());
        return interviewRecordMapper.insertInterviewRecord(interviewRecord);
    }

    /**
     * 修改模拟面试记录

     * 
     * @param interviewRecord 模拟面试记录

     * @return 结果
     */
    @Override
    public int updateInterviewRecord(InterviewRecord interviewRecord)
    {
        interviewRecord.setUpdateTime(DateUtils.getNowDate());
        return interviewRecordMapper.updateInterviewRecord(interviewRecord);
    }

    /**
     * 批量删除模拟面试记录

     * 
     * @param ids 需要删除的模拟面试记录
主键
     * @return 结果
     */
    @Override
    public int deleteInterviewRecordByIds(Long[] ids)
    {
        return interviewRecordMapper.deleteInterviewRecordByIds(ids);
    }

    /**
     * 删除模拟面试记录
信息
     * 
     * @param id 模拟面试记录
主键
     * @return 结果
     */
    @Override
    public int deleteInterviewRecordById(Long id)
    {
        return interviewRecordMapper.deleteInterviewRecordById(id);
    }

    /**
     * 生成模拟面试记录
     *
     * @param positionId 职位id
     * @param message 职位描述
     * @return 模拟面试记录id
     */
    @Override
    public Long generateRecord(Long positionId, String message) {
        // 1、添加面试记录，拿到新添加的面试记录的ID
        // 1.1 获取到当前登录的微信用户ID  TODO
        Long wxUserId = SecurityUtils.getLoginUser().getUserId();
        // 1.2 创建并存储面试记录
        InterviewRecord record = new InterviewRecord();
        //存储id
        record.setUserId(wxUserId);
        record.setPositionId(positionId);
        //存储面试记录
        interviewRecordMapper.insertInterviewRecord(record);
        // 2、添加问题
        DeepseekResponseData data = JSONObject.parseObject(message,DeepseekResponseData.class);//解析接口返回的json数据
        List<DeepQuestionVo> questions = data.getQuestions();
        //创建面试问题列表
        List<InterviewQuestion> interviewQuestions = new ArrayList<>();
        for (int i = 0; i < questions.size(); i++) {
            DeepQuestionVo question = questions.get(i);
            InterviewQuestion interviewQuestion = new InterviewQuestion();
            interviewQuestion.setRecordId(record.getId());
            interviewQuestion.setQuestionNo((long)i);
            interviewQuestion.setContent(question.getText());
            interviewQuestion.setQuestionPrompt(question.getSample_answer());
            interviewQuestion.setCreateTime(new Date());
            interviewQuestions.add(interviewQuestion);
        }
        interviewQuestionMapper.insertInterviewQuestionBatch(interviewQuestions);
        // 3、返回面试记录ID
        return record.getId();
    }

    /**
     * 开始面试
     *
     * @param recordId 面试记录ID
     * @return 面试题目列表
     */
    @Override
    public List<InterviewQuestion> start(Long recordId) {
        // 1、修改面试记录状态
        InterviewRecord record = interviewRecordMapper.selectInterviewRecordById(recordId);
        record.setStartTime(new Date());//设置开始时间
        record.setStatus("0");//设置状态为进行中
        interviewRecordMapper.updateInterviewRecord(record);
        // 2、查询面试题目
        InterviewQuestion query = new InterviewQuestion();
        query.setRecordId(record.getId());
        List<InterviewQuestion> interviewQuestions = interviewQuestionMapper.selectInterviewQuestionList(query);
        return interviewQuestions;
    }

    @Override
    public List<InterviewQuestion> end(Long recordId) {
        // 1、修改面试记录状态
        InterviewRecord record = interviewRecordMapper.selectInterviewRecordById(recordId);
        record.setEndTime(new Date());//设置结束时间
        record.setStatus("1");//设置状态为进行中
        interviewRecordMapper.updateInterviewRecord(record);
        // 2、查询面试题目
        InterviewQuestion query = new InterviewQuestion();
        query.setRecordId(record.getId());
        List<InterviewQuestion> interviewQuestions = interviewQuestionMapper.selectInterviewQuestionList(query);
        return interviewQuestions;
    }


}
